{{template "admin/header.html"}}
<title>新增导航菜单 - GPRESS</title>
{{template "admin/bodystart.html"}}


{{ $pageTemplate := pageTemplate }} <!-- 模版数据 -->

{{ $categoryName := selectOne "category" "* from category WHERE id=?" .QueryStringMap.pid }}

{{ $maxSortNo:=  selectOne "category" "max(sortNo) as sortNo from category" }} 

<section class="section">
	<input type="hidden" id="basePath" value="{{basePath}}">

	<div class="container">
		<div class="table-content">
			<!-- is-6-desktop is-6-tablet -->
			<div class="column">
				<form id="gpress-form" action="{{basePath}}admin/{{.UrlPathParam}}/save" method="POST">
					<ul class="save-ul">
						<li>
							<div class="left">
								ID
							</div>
							<div class="right">
								<input class="input is-small" type="text" name="id" id="id" value="{{generateStringID}}"/>
							</div>
						</li>
						<li>
							<div class="left">
								导航名称
							</div>
							<div class="right">
								<input class="input is-small" type="text" name="name" id="name">
							</div>
						</li>
						<li>
							<div class="left">
								跳转路径
							</div>
							<div class="right">
								<input class="input is-small" type="text" name="hrefURL" id="hrefURL">
							</div>
						</li>
						<li>
							<div class="left">
								跳转方式
							</div>
							<div class="select is-small">
								<select name="hrefTarget" id="hrefTarget">
									<option value="">默认</option>
									<option value='_blank'>_blank</option>
									<option value='_parent'>_parent</option>
									<option value='_self'>_self</option>
									<option value='_top'>_top</option>
								</select>
							</div>

						</li>
						<li>
							<div class="left">
								父级导航
							</div>
							<div class="right">
								<input  type="hidden"  name="pid"  value="{{$categoryName.Id}}">
								<input class="input is-small" type="text" value="{{$categoryName.Name}}" disabled>
							</div>
						</li>
						<li>
							<div class="left">
								页面模版
							</div>
							<div class="select is-small">
								<select id="templateSelect">
									<option value="">默认(category.html)</option>
									{{ range $index,$obj := $pageTemplate }}
									<option value='{{$obj.Id}}'>{{$obj.Name}}</option>
									{{end}}
								</select>
							</div>
						</li>
						<li>
							<div class="left">
								排序
							</div>
							<div class="right">
								<input class="input is-small" type="number" name="sortNo" id="sortNo" value="{{addInt $maxSortNo.SortNo 1}}" />
							</div>
						</li>
						<li>
							<div class="left">
								状态
							</div>
							<div class="select is-small">
								<select name="status" id="status">
								  <option value="1">公开</option>
								  <option value="0">列表不显示</option>
								  <option value="2">私密</option>
								</select>
							</div>
						</li>
					</ul>
					<div class="field is-grouped" style="padding-left: 190px;">
						<div class="control">
							<button type="submit" class="button is-success is-small">提交</button>
						</div>
						<div class="control">
							<button type="reset" class="button is-info is-light is-small">重置</button>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
</section>

<script>
	let templateID = ''
	$('#templateSelect').change(function(e) {
		templateID = $('#templateSelect').val()
	})
	// 保存提交
	const form = document.querySelector('#gpress-form')
	form.addEventListener('submit', async event => {
		event.preventDefault()
		const formData = new FormData(form)
		const jsonObject = {}
		for (const [key, value] of formData.entries()) {
			const fieldType = document.querySelector(key)
				jsonObject[key] = value
		}
		jsonObject.sortNo = jsonObject.sortNo-0
		jsonObject.status = jsonObject.status-0
		jsonObject.templateID = templateID
		const response = await fetch(form.action, {
			method: form.method,
			body: JSON.stringify(jsonObject),
			headers: { 'Content-Type': 'application/json' }
		})
		let res = await response.json()
		if (res.statusCode == 1) {
			$.confirm({
				useBootstrap: false,
				boxWidth: '400px',
				title: '提示',
				content: '保存成功！',
				type: 'green',
				buttons: {
					'确定': function() {
						window.location.href = '{{basePath}}admin/content/list'
					}
				}
			})
		}
	})
</script>


{{template "admin/bodyend.html"}}
